home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / vendor.cl_ / vendor.cl
Text File  |  1994-11-20  |  7KB  |  245 lines

  1. Version 1.0 Class
  2. Attribute VB_Name = "clsVendor"
  3. Attribute VB_Creatable = True
  4. Attribute VB_Exposed = False
  5. Option Explicit
  6.  
  7. ' Vendor number is an integer. It must be a positive integer (negative
  8. ' numbers would make no sense. These constants define the legal limits
  9. ' of a positive integer.
  10.  
  11. Const MIN_VENDORNUMBER = 1
  12. Const MAX_VENDORNUMBER = 32767
  13.  
  14. ' These are the private variables used to hold the object properties.
  15. ' Each is readable by outside routines via Property Get routines but is
  16. ' not directly modifiable by outside routines.
  17.  
  18. Private propNumber As Integer
  19. Private propCompany As String
  20. Private propAddress As String
  21. Private propFEIN As String
  22.  
  23. ' The propDelimitedString variable is not readable by outside routines but
  24. ' can be set by a Property Let statement.
  25.  
  26. Private propDelimitedString As String
  27.  
  28.  
  29. '*****************************************************************************
  30. Property Let DelimitedString(delim As String)
  31.  
  32.         ' Stores the passed argument as the DelimitedString property
  33.         ' Each string represents one record. This routine parses
  34.         ' the string into fields and stores the field values as the
  35.         ' appropriate object properties.
  36.  
  37.     Dim delimiter As String
  38.     Dim endPos As Integer, fieldNum As Integer, startPos As Integer
  39.     Dim i As Integer
  40.     Dim textNumber As String
  41.  
  42.         ' Define the string used to delimit fields within a record.
  43.  
  44.     delimiter = Chr$(9)
  45.  
  46.         ' Store the argument as a property.
  47.  
  48.     propDelimitedString = delim
  49.  
  50.         ' Initialize the parse routine.
  51.  
  52.     startPos = 1
  53.     fieldNum = 1
  54.  
  55.     ' Go through the string. Extract each field. Stop when the maximum
  56.     ' number of fields is reached or the string ends. (If the input data
  57.     ' is correct, the string should never end before the maximum number
  58.     ' of fields is reached.)
  59.  
  60.     Do
  61.  
  62.         ' Find the position of the delimiter character in the string,
  63.         ' starting from the character after the last delimiter found.
  64.  
  65.         endPos = InStr(startPos, delim, delimiter)
  66.  
  67.         ' If we didn't find one, set the position to the end of the string.
  68.  
  69.         If endPos = 0 Then endPos = Len(delim) + 1
  70.  
  71.         ' Process the field that the text between startPos and endPos
  72.         ' represents.
  73.  
  74.         Select Case fieldNum
  75.  
  76.             Case 1
  77.  
  78.                     ' Extract the text enclosed by startPos and endPos.
  79.  
  80.                     textNumber = ExtractField(startPos, endPos)
  81.  
  82.                     ' Check to make sure this text represents a legal Vendor
  83.                     ' Number. If it doesn't. set the value of the vendor number
  84.                     ' to zero. This allows the remainder of the record to be
  85.                     ' processed. Outside routines can use Property Get Number
  86.                     ' to check to make sure the vendor number is non-zero.
  87.  
  88.                     If IsNumeric(textNumber) Then
  89.                             If textNumber >= MIN_VENDORNUMBER And textNumber _
  90.                             <= MAX_VENDORNUMBER Then
  91.                                 propNumber = Val(textNumber)
  92.                             Else
  93.                                 propNumber = 0
  94.                             End If
  95.                     Else
  96.                         propNumber = 0
  97.                     End If
  98.  
  99.             Case 2
  100.                 propCompany = ExtractField(startPos, endPos)
  101.  
  102.             Case 3
  103.                 propAddress = ExtractField(startPos, endPos)
  104.  
  105.             Case 4
  106.                 propFEIN = ExtractField(startPos, endPos)
  107.  
  108.         End Select
  109.  
  110.         ' Set the startPos for the next cycle to the character past the
  111.         ' existing endPos.
  112.  
  113.         startPos = endPos + 1
  114.  
  115.         ' Increment the fieldNum loop control variable.
  116.  
  117.         fieldNum = fieldNum + 1
  118.  
  119.         ' If we're not at the end of the string or the end of the field list,
  120.         ' keep going.
  121.  
  122.     Loop While endPos < Len(delim) And fieldNum <= 4
  123.  
  124. End Property
  125.  
  126. '*****************************************************************************
  127. Public Function StoreNewItem(rs As Recordset) As Boolean
  128.  
  129.     ' Saves the value of the object as a new record in the recordset passed
  130.     ' as the argument. Returns True if it succeeds, False if an error occurs.
  131.  
  132.     On Error GoTo StoreNewError
  133.  
  134.     ' Prepare to add a new record.
  135.  
  136.     rs.AddNew
  137.  
  138.     ' Call WriteItem to update the values. If WriteItem, it encountered no
  139.     ' errors, so go ahead and save the record. It WriteItem returns False,
  140.     ' there was an error, so go to the error handler.
  141.  
  142.     If WriteItem(rs) Then
  143.         rs.Update
  144.     Else
  145.         GoTo StoreNewError
  146.     End If
  147.  
  148.     ' Set the return value to indicate success.
  149.  
  150.     StoreNewItem = True
  151.  
  152. Exit Function
  153.  
  154. StoreNewError:
  155.  
  156.     ' Set the return value to indicate an error and return to the calling
  157.     ' routine.
  158.  
  159.     StoreNewItem = False
  160.  
  161. Exit Function
  162.  
  163. End Function
  164.  
  165. '*****************************************************************************
  166. Private Function WriteItem(rs As Recordset) As Boolean
  167.  
  168.     ' Assigns the current property values to the Vendor table fields in
  169.     ' the current record. Returns True unless an error is encountered.
  170.  
  171.     ' Set up the error handler.
  172.  
  173.     On Error GoTo WriteItemError
  174.  
  175.     ' Assign property values to table fields.
  176.  
  177.     rs("Vendor Number") = propNumber
  178.     rs("Name") = propCompany
  179.     rs("Address") = propAddress
  180.     rs("FEIN") = propFEIN
  181.  
  182.     ' Return True to indicate no errors.
  183.  
  184.     WriteItem = True
  185.  
  186. Exit Function
  187.  
  188. WriteItemError:
  189.  
  190.     ' Return False to indicate an error.
  191.  
  192.     WriteItem = False
  193.  
  194. Exit Function
  195.  
  196. End Function
  197.  
  198. '*****************************************************************************
  199. Private Function ExtractField(startPos As Integer, endPos As Integer)
  200.  
  201.     ' Returns the text from propDelimitedString between the startPos and
  202.     ' endPos positions.
  203.  
  204.     ExtractField = Mid$(propDelimitedString, startPos, (endPos - startPos))
  205.  
  206. End Function
  207.  
  208. '*****************************************************************************
  209. Property Get Number() As Integer
  210.  
  211.     ' Returns the current value of propVendorNumber
  212.  
  213.     Number = propNumber
  214.  
  215. End Property
  216.  
  217. '*****************************************************************************
  218. Property Get Company() As String
  219.  
  220.     ' Returns the current value of propCompany.
  221.  
  222.     Company = propCompany
  223.  
  224. End Property
  225.  
  226. '*****************************************************************************
  227. Property Get Address() As String
  228.  
  229.     ' Returns the current value of propAddress.
  230.  
  231.     Address = propAddress
  232.  
  233. End Property
  234.  
  235. '*****************************************************************************
  236. Property Get FEIN() As String
  237.  
  238.     ' Returns the current value of propFEIN
  239.  
  240.  
  241.     FEIN = propFEIN
  242.  
  243. End Property
  244.  
  245.